#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

typedef struct 
{
    void *buf;
    size_t sz;
    size_t pos;
    void (*set_at)(void*, size_t, double);
    void (*buf_push)(void*, double);
} Buffer;

void set_at(void* buffer, size_t ix, double val)
{
    Buffer* buf = (Buffer*)buffer;
    double *dp = (double*)buf->buf;
    dp += ix;
    *dp = val;
}

void buf_push(void* buffer, double val)
{
    Buffer* buf = (Buffer*)buffer;
    if (buf->pos == buf->sz)
    {
        void *newBuf = malloc(buf->sz * 2 * 8);
        memset(newBuf, 0, buf->sz * 2 * 8);
        memcpy(newBuf, buf->buf, buf->sz * 8);
        free(buf->buf);
        buf->buf = newBuf;
        buf->sz *= 2;
    }
    double *dp = (double*)buf->buf;
    dp += buf->pos;
    *dp = val;
    ++buf->pos;
}
// 初始化 Buffer 类对象
void initBuffer(Buffer* buffer) {
    buffer->pos = 0;
    buffer->sz = 512;
    buffer->buf = malloc(buffer->sz * 8);
    buffer->set_at = set_at;
    buffer->buf_push = buf_push;
}



